{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "13fa1087",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: openai in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (0.28.0)\n",
      "Collecting openai\n",
      "  Obtaining dependency information for openai from https://files.pythonhosted.org/packages/bf/6b/7d0b41c8fc44044c251da6d3e33a4b8f60ab5e10f66db8b0ebc22e40fd90/openai-1.3.6-py3-none-any.whl.metadata\n",
      "  Downloading openai-1.3.6-py3-none-any.whl.metadata (17 kB)\n",
      "Requirement already satisfied: anyio<4,>=3.5.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (3.5.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.8.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (0.24.1)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.10.12)\n",
      "Requirement already satisfied: sniffio in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (1.2.0)\n",
      "Requirement already satisfied: tqdm>4 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (4.65.0)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from openai) (4.7.1)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
      "Requirement already satisfied: certifi in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
      "Requirement already satisfied: httpcore<0.18.0,>=0.15.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai) (0.17.3)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from httpcore<0.18.0,>=0.15.0->httpx<1,>=0.23.0->openai) (0.14.0)\n",
      "Downloading openai-1.3.6-py3-none-any.whl (220 kB)\n",
      "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m220.9/220.9 kB\u001b[0m \u001b[31m717.6 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
      "\u001b[?25hInstalling collected packages: openai\n",
      "  Attempting uninstall: openai\n",
      "    Found existing installation: openai 0.28.0\n",
      "    Uninstalling openai-0.28.0:\n",
      "      Successfully uninstalled openai-0.28.0\n",
      "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n",
      "semantic-kernel 0.3.14.dev0 requires openai<0.29,>=0.27, but you have openai 1.3.6 which is incompatible.\u001b[0m\u001b[31m\n",
      "\u001b[0mSuccessfully installed openai-1.3.6\n"
     ]
    }
   ],
   "source": [
    "!pip install openai==1.3.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d7ea1c13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: openai\n",
      "Version: 1.3.6\n",
      "Summary: The official Python library for the openai API\n",
      "Home-page: \n",
      "Author: \n",
      "Author-email: OpenAI <support@openai.com>\n",
      "License: \n",
      "Location: /Users/mbj0593/anaconda3/lib/python3.11/site-packages\n",
      "Requires: anyio, distro, httpx, pydantic, sniffio, tqdm, typing-extensions\n",
      "Required-by: semantic-kernel\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip show openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c034927",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import os\n",
    "#os.environ[\"OPENAI_API_TYPE\"]='azure'\n",
    "#os.environ[\"OPENAI_API_VERSION\"]='2023-08-01-preview'\n",
    "#os.environ[\"OPENAI_API_BASE\"]='https://<Your EndPoint>/'\n",
    "#os.environ[\"OPENAI_API_KEY\"]='<Your Key>''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d08aa43a",
   "metadata": {},
   "outputs": [],
   "source": [
    "deployment = \"gpt4\"\n",
    "model = \"gpt-4\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31f410fa",
   "metadata": {},
   "source": [
    "# Hello GPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5dd7f3da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！作为一个AI，我没有个人名字，但你可以叫我Assistant或者任何你喜欢的名字。怎么称呼你？有什么我可以帮助你的吗？\n",
      "ChatCompletion(id='chatcmpl-8QSUUYBVeMaQrgAQmRKUJz5vmLxHC', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='你好！作为一个AI，我没有个人名字，但你可以叫我Assistant或者任何你喜欢的名字。怎么称呼你？有什么我可以帮助你的吗？', role='assistant', function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})], created=1701316738, model='gpt-4', object='chat.completion', system_fingerprint='fp_50a4261de5', usage=CompletionUsage(completion_tokens=58, prompt_tokens=30, total_tokens=88), prompt_filter_results=[{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}])\n"
     ]
    }
   ],
   "source": [
    "# 以下是直接使用 OpenAI 的方式\n",
    "\n",
    "#from openai import OpenAI\n",
    "#client = OpenAI()\n",
    "\n",
    "import os\n",
    "from openai import AzureOpenAI\n",
    "    \n",
    "client = AzureOpenAI(\n",
    "    api_key=os.getenv(\"OPENAI_API_KEY\"),  \n",
    "    api_version=os.getenv(\"OPENAI_API_VERSION\"),\n",
    "    azure_endpoint = os.getenv(\"OPENAI_API_BASE\")\n",
    "    )\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "    model=deployment, \n",
    "     messages=[\n",
    "        {\"role\": \"system\", \"content\": \"你是一个AI助理\"},   \n",
    "        {\"role\": \"user\", \"content\": \"你好！你叫什么名字？\"}\n",
    "    ],\n",
    "    temperature = 0.9, \n",
    "    max_tokens = 1000,)\n",
    "print(response.choices[0].message.content)\n",
    "\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c95c2c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['你好！作为一个AI助理，我没有一个个人的名字，但你可以随意给我取一个名字，或者简单地称呼我为AI助理或AI。怎样称呼我都可以，最重要的是我在这里帮助你。需要我做些什么吗？']\n"
     ]
    }
   ],
   "source": [
    "print([choice.message.content for choice in response.choices])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d9ff976",
   "metadata": {},
   "source": [
    "# Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3b04096b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tiktoken in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (0.4.0)\r\n",
      "Requirement already satisfied: regex>=2022.1.18 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from tiktoken) (2022.7.9)\r\n",
      "Requirement already satisfied: requests>=2.26.0 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from tiktoken) (2.31.0)\r\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (2.0.4)\r\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (3.4)\r\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/mbj0593/anaconda3/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install tiktoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54b967e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[19000, 39442, 37507, 98806, 81543, 28037, 37507, 9554, 13646, 20022, 247, 3922, 60843, 31634, 19361, 17792, 24326, 232, 8676, 225, 6701, 249, 67178, 20834, 37507, 3922, 45932, 96, 19483, 17792, 51611, 76982, 21043, 98739, 1811]\n",
      "chinese:在未来还没有到来的时候，总要有人把它创造出来，那个人应该是我们。 ; 35 tokens\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import openai\n",
    "import tiktoken\n",
    "\n",
    "encoding = tiktoken.encoding_for_model(\"gpt-4\")\n",
    "\n",
    "chinese = \"\"\"在未来还没有到来的时候，总要有人把它创造出来，那个人应该是我们。\"\"\"\n",
    "\n",
    "tokens = encoding.encode(chinese)\n",
    "\n",
    "print(tokens)\n",
    "\n",
    "num_of_tokens_in_chinese = len(encoding.encode(chinese))\n",
    "\n",
    "print(f\"chinese:{chinese} ; {num_of_tokens_in_chinese} tokens\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea48fe92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
